'use strict';

const { Service } = require('egg');
const ModelName = 'User'
class RouterService extends Service {
    /**
     * 根据分页和搜索条件获取
     * @param {Object} where 查询用户的条件
     * @param {Number} page 当前分页
     * @param {Number} pageSize 当前页每页多少条
     */
    async getPage(where, page, pageSize) {
        return this.ctx.model[ModelName].findAndCountAll({
            where,
            offset: pageSize * (page - 1),
            limit: pageSize,
            order:[
                // id 降序
                ['create_time','ASC']
            ],
        });
    }

    // 所有列表
    async allList(where) {
        return this.ctx.model[ModelName].findAll({
            where,
            order:[
                // id 降序
                ['create_time','ASC']
            ],
        });
    }
    async findOne(id) {
        return this.ctx.model[ModelName].findOne({
            where: {
                id,
            },
        });
    }

    /**
     * 创建
     * @param {*} data 角色信息
     */
    async create(data) {
        let where = {
            phone: data.phone,
            role: data.role,
        }
        if (data.role == 2) {
            where.company_name = data.company_name;
        }
        // 判断账号是否存在
        const user = await this.ctx.model[ModelName].findOne({
            where: { phone: data.phone, role: data.role },
        })
        if (user) throw new Error('账号已存在');
        const insert = await this.ctx.model[ModelName].create({
            ...data,
            nickname: data.nickname || data.phone.slice(0, 3) + '****' + data.phone.slice(-4),
        });
        return this.findOne(insert.id);
    }
    async login({ phone, password }) {
        console.log('----')
        // 根据账号查询用户是否存在
        let user = await this.ctx.model[ModelName].findOne({
            where: { phone, role: 1 },
        });
        if (user.status === 0) {
            throw new Error('账号已被禁用');
        }
        if (!user) {
            // 直接注册
            user = await this.create({ phone, password, role: 1 });
        } else {
            // 判断密码是否有误
            const isValid = user.password === password
            if (!isValid) {
                throw new Error('密码错误');
            }
        }
        user = user.dataValues;
        delete user.password;
        return user;
    }
    async companyLogin({ phone, password }) {
        // 根据账号查询用户是否存在
        let user = await this.ctx.model[ModelName].findOne({
            where: { phone, role: 2 },
        });
        if (!user) throw new Error('账号不存在');
        if (user.status === 0) {
            throw new Error('账号已被禁用');
        }
        if (user.audit === -1) {
            throw new Error('账号待审核');
        }
        if (user.audit === 0) {
            throw new Error('账号审核未通过');
        }
        // 判断密码是否有误
        const isValid = user.password === password
        if (!isValid) {
            throw new Error('密码错误');
        }
        user = user.dataValues;
        delete user.password;
        return user;
    }

    /**
     * 根据ID修改
     * @param {Number} id ID
     * @param {Object} data 要修改的信息
     */
    async update(id, data) {
        return await this.ctx.model[ModelName].update(data, { where: { id } });
    }

    /**
     * 根据id删除
     * @param {Number}
     */
    async deleteById(id) {
        return await this.ctx.model[ModelName].destroy({ where: { id } });
    }
}

module.exports = RouterService;
